package com.bbcare.followup.plat.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.DateUtil;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.PropertiesUtils;
import com.bbcare.comm.QueryModel;
import com.bbcare.comm.ResultModel;
import com.bbcare.comm.SeqUtil;
import com.bbcare.followup.plat.entity.Patient;
import com.bbcare.followup.plat.service.IAppUserService;
import com.bbcare.followup.plat.service.IPatientService;
import com.bbcare.followup.plat.service.ISmsService;

/**
 * @name: app端调用随访系统控制类
 * @author: DemonLee
 * @createTime: 2017.5.19
 * @description: 随访系统提供给app端调用的相关API
 * @modify:
 * 
 */
@Controller
@RequestMapping("/webapi/service/appuser")
public class AppUserSeviceController {

    Logger logger = Logger.getLogger(AppUserSeviceController.class);

    @Autowired
    private IAppUserService appUserService;

    @Autowired
    private IPatientService patientSerivce;

    @Autowired
    private ISmsService smsService;

    /**
     * 查询主诉（登记）任务内容
     * 
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ptContDataQuery")
    public ResultModel<Map<String, Object>> getPtContData(@RequestBody String jsonStr) throws Exception {
        logger.info("in getPtContData----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理

        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String channel = requestParam.getString("channel");
        if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
        }

        String patientId = requestParam.getString("patientId");
        if (StringUtils.isEmpty(patientId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，患者不能为空!");
        }
        String queueId = requestParam.getString("queueId");
        if (StringUtils.isEmpty(queueId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，队列id不能为空!");
        }
        String type = requestParam.getString("type");
        if (StringUtils.isEmpty(type)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，问卷类型不能为空!");
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("patientId", patientId);
        map.put("invokeType", "1");// app调用
        map.put("queueId", queueId);
        map.put("type", type);
        map.put("custValType", "ayd");// 安医大项目特殊标识
        Map<String, Object> rspMap = appUserService.getAppPtTaskContDataInfo(map);

        return new ResultModel<Map<String, Object>>(rspMap);
    }

    /**
     * 提交主诉（线上随访）任务内容
     * 
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ptContDataQuerySubmit")
    public ResultModel<HashMap<String, Object>> savePtContDataQuery(@RequestBody String arg) throws Exception {
        logger.warn("in savePtContDataQuerys----arg==" + arg);

        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参错误，非json格式！"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }
        JSONObject jsonTask = jsonData.getJSONObject("task");
        if (null == jsonTask) {
            // 前台必须传task节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无task节点！"));
        }
        JSONObject jsonOpInfo = jsonData.getJSONObject("oprInfo");
        if (null == jsonOpInfo) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无oprInfo节点！"));
        }
        String opCode = jsonOpInfo.getString("opCode");
        if (null == opCode || "".equals(opCode)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无操作编码节点！"));
        }
        String queueId = jsonOpInfo.getString("followQueueId");
        if (null == queueId || "".equals(queueId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无队列id节点！"));
        }
        String ptSchemeId = jsonOpInfo.getString("ptSchemeId");
        if (null == ptSchemeId || "".equals(ptSchemeId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
                    "入参节点错误，未传入患者队列实例id或值为空！"));
        }
        String userId = jsonOpInfo.getString("patientId");
        if (null == userId || "".equals(userId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，患者id未传或值为空！"));
        }

        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        // 创建人，是患者自己
        String author = userId;
        Map<String, Object> ptMap = new HashMap<String, Object>();
        ptMap.put("userId", userId);
        ptMap.put("tenantId", Constants.PT_USER_INFO_DEFAULT_TENANT_ID);
        List<Patient> ptList = patientSerivce.getPatientList(ptMap);
        if (CollectionUtils.isEmpty(ptList)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_PATIENT_NOT_EXIST, "查询随访系统无此患者！"));
        }
        Patient ptVo = ptList.get(0);
        String authorName = ptVo.getUserName();
        String tenantId = null;// 租户id
        paramMap.put("authorName", authorName);

        String taskId = jsonTask.getString("ptTaskId");
        if (null == taskId || "".equals(taskId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
                    "入参节点错误，未传入患者任务id或值为空！"));
        }
        String type = jsonTask.getString("type");
        if (StringUtils.isEmpty(type)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，问卷类型不能为空!");
        }

        String currTime = DateUtil.getTimeString(DateUtil.getSysDateA());// 当前时间
        String updateAccept = SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID);// 操作流水
        paramMap.put("taskId", taskId);
        paramMap.put("contentData", jsonData.getJSONObject("questiones"));
        paramMap.put("ptSchemeId", ptSchemeId);
        paramMap.put("userId", userId);
        paramMap.put("updateTime", currTime);
        paramMap.put("updateAccept", updateAccept);
        paramMap.put("authorId", author);
        paramMap.put("tenantId", tenantId);
        paramMap.put("opCode", opCode);
        paramMap.put("queueId", queueId);
        paramMap.put("opNote", jsonOpInfo.getString("opNote"));
        paramMap.put("opType", jsonOpInfo.getString("opType"));
        paramMap.put("updateAuname", authorName);
        paramMap.put("type", type);
        paramMap.put("invokeType", "1");// app调用

        patientSerivce.modTaskContentData(paramMap);

        HashMap<String, Object> retMap = new HashMap<String, Object>();
        return new ResultModel<HashMap<String, Object>>(retMap);
    }

    /**
     * 根据随访系统的患者id，查询该患者在随访系统中加入的队列list
     * 
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ptQueueListQuery")
    public ResultModel<Map<String, Object>> listPtQueueInfo(@RequestBody String jsonStr) throws Exception {
        logger.info("in listPtQueueInfo----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理

        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String channel = requestParam.getString("channel");
        if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
        }

        String patientId = requestParam.getString("patientId");
        if (StringUtils.isEmpty(patientId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，患者不能为空!");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("patientId", patientId);
        map.put("invokeType", "1");// app调用
        if (!StringUtils.isEmpty(requestParam.getString("departmentId"))) {
            map.put("departmentId", requestParam.getString("departmentId"));
        }
        Map<String, Object> rspMap = appUserService.listPtQueueInfo(map);

        return new ResultModel<Map<String, Object>>(rspMap);
    }

    /**
     * 线上随访提醒查询
     * 
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ptTaskInformQuery")
    public ResultModel<Map<String, Object>> getPtTaskInform(@RequestBody String jsonStr) throws Exception {
        logger.info("in getPtTaskInform----arg==" + jsonStr);

        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理

        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String channel = requestParam.getString("channel");
        if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
        }

        String patientId = requestParam.getString("patientId");
        if (StringUtils.isEmpty(patientId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，患者不能为空!");
        }
        String groupType = requestParam.getString("groupType");
        if (StringUtils.isEmpty(groupType)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，组织类型不能为空!");
        }
        String groupId = requestParam.getString("groupId");
        if (StringUtils.isEmpty(groupId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，组织id不能为空!");
        }
        String quesType = requestParam.getString("quesType");
        if (StringUtils.isEmpty(quesType)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，问卷类型不能为空!");
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("patientId", patientId);
        map.put("invokeType", "1");// app调用
        if (Constants.ID_FOR_OFFICE.equals(groupType)) {
            map.put("departmentId", groupId);
        } else if (Constants.ID_FOR_QUEUE.equals(groupType)) {
            map.put("queueId", groupId);
        } else {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，组织类型值未定义!");
        }
        map.put("type", quesType);
        Map<String, Object> rspMap = appUserService.getPtOnlineTaskInform(map);

        return new ResultModel<Map<String, Object>>(rspMap);
    }

    /**
     * 查询线上随访任务内容
     * 
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ptInformContDataQuery")
    public ResultModel<Map<String, Object>> getPtInformContData(@RequestBody String jsonStr) throws Exception {
        logger.info("in getPtInformContData----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理

        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String channel = requestParam.getString("channel");
        if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
        }

        String patientId = requestParam.getString("patientId");
        if (StringUtils.isEmpty(patientId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，患者id不能为空!");
        }
        String queueId = requestParam.getString("followQueueId");
        if (StringUtils.isEmpty(queueId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，队列id不能为空!");
        }
        String type = requestParam.getString("quesType");
        if (StringUtils.isEmpty(type)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，问卷类型不能为空!");
        }
        String ptSchemeId = requestParam.getString("ptSchemeId");
        if (StringUtils.isEmpty(ptSchemeId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，患者队列实例id不能为空!");
        }
        String ptTaskId = requestParam.getString("ptTaskId");
        if (StringUtils.isEmpty(ptTaskId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，患者任务实例id不能为空!");
        }
        String seqNo = requestParam.getString("seqNo");
        if (StringUtils.isEmpty(seqNo)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，患者任务序号不能为空!");
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("patientId", patientId);
        map.put("invokeType", "1");// app调用
        map.put("queueId", queueId);
        map.put("type", type);
        map.put("ptSchemeId", ptSchemeId);
        map.put("ptTaskId", ptTaskId);
        map.put("seqNo", seqNo);
        Map<String, Object> rspMap = appUserService.getPtInformOnlineContData(map);

        return new ResultModel<Map<String, Object>>(rspMap);
    }

    /**
     * 通用接口，查询线上主诉登记、线上随访的任务内容
     * 
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ptPubOnlineContDataQuery")
    public ResultModel<Map<String, Object>> getpPtPubOnlineContData(@RequestBody String jsonStr) throws Exception {
        logger.info("in getpPtPubOnlineContData----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理

        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String channel = requestParam.getString("channel");
        if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
        }

        String patientId = requestParam.getString("patientId");
        if (StringUtils.isEmpty(patientId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，患者不能为空!");
        }
        String queueId = requestParam.getString("queueId");
        if (StringUtils.isEmpty(queueId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，队列id不能为空!");
        }
        String type = requestParam.getString("type");
        if (StringUtils.isEmpty(type)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，问卷类型不能为空!");
        }
        if (!"1".equals(type) && !"8".equals(type)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，问卷类型传值有误!");
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("patientId", patientId);
        map.put("invokeType", "1");// app调用
        map.put("queueId", queueId);
        map.put("type", type);
        Map<String, Object> rspMap = appUserService.getOnlinePtTaskContDataInfo(map);

        return new ResultModel<Map<String, Object>>(rspMap);
    }

    // app端患者操作记录查询（如主诉登记等）
    @ResponseBody
    @RequestMapping("/ptOnlineOperateListQuery")
    public ResultModel<Map<String, Object>> listPtOnlineOperateList(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in listPtOnlineOperateList----arg==" + arg);

        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
        }

        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
        }
        String channel = jsonData.getString("channel");
        if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
        }

        HashMap<String, Object> paramMap = new HashMap<String, Object>();

        String flag = jsonData.getString("flag");
        if (StringUtils.isEmpty(flag)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无flag节点或值为空！"));
        }
        String patientId = jsonData.getString("patientId");
        if (StringUtils.isEmpty(patientId)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无患者id节点或值为空！"));
        }

        // 安医大app查询主诉登记特殊处理（无患者自助填写记录，则查询门诊记录）
        Map<String, Object> qryUserIn = new HashMap<String, Object>();
        qryUserIn.put("revTenantId", Constants.PT_USER_INFO_DEFAULT_TENANT_ID);
        qryUserIn.put("userId", patientId);// app侧必传
        qryUserIn.put("start", 0);
        qryUserIn.put("end", 1);
        List<Patient> appPtList = patientSerivce.getPatientList(qryUserIn);
        if (!CollectionUtils.isEmpty(appPtList)) {
            Patient appPtVo = appPtList.get(0);
            String appTenantId = appPtVo.getTenantId();
            if (Constants.TENANT_ID_FOR_AYD_MAS.equals(appTenantId)) {
                paramMap.put("appTenantId", appTenantId);
            }
        }

        String phoneNo = jsonData.getString("phoneNum");
        String userName = jsonData.getString("patientName");// 可以是姓氏或姓名
        int page = jsonData.getIntValue("page");
        int rows = jsonData.getIntValue("rows");
        page = page == 0 ? 1 : page;
        rows = rows == 0 ? 20 : rows;

        if (null != phoneNo && !"".equals(phoneNo)) {
            paramMap.put("phoneNo", phoneNo);
        }
        if (null != userName && !"".equals(userName)) {
            paramMap.put("userName", userName);
        }
        paramMap.put("flag", flag);
        paramMap.put("start", (page - 1) * rows);
        paramMap.put("end", rows);
        if (!StringUtils.isEmpty(jsonData.getString("hisNo"))) {
            paramMap.put("hisNo", jsonData.getString("hisNo"));
        }
        if (!StringUtils.isEmpty(jsonData.getString("queueId"))) {
            paramMap.put("queueId", jsonData.getString("queueId"));
        }
        if (!StringUtils.isEmpty(jsonData.getString("qryStartTime"))) {
            paramMap.put("qryStartTime", jsonData.getString("qryStartTime"));
        }
        if (!StringUtils.isEmpty(jsonData.getString("qryEndTime"))) {
            paramMap.put("qryEndTime", jsonData.getString("qryEndTime"));
        }
        paramMap.put("invokeType", "1");// app调用
        paramMap.put("patientId", patientId);
        paramMap.put("taskNotNull", "1");

        HashMap<String, Object> retMap = patientSerivce.qryPatientOperationList(paramMap);

        return new ResultModel<Map<String, Object>>(retMap);
    }

    /**
     * 查询主诉（登记）任务内容操作详情
     * 
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ptOnlineOperateDetailQuery")
    public ResultModel<Map<String, Object>> getPtOnlineOperateDetail(@RequestBody String jsonStr) throws Exception {
        logger.info("in getPtOnlineOperateDetail----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理

        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String channel = requestParam.getString("channel");
        if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
        }

        String patientId = requestParam.getString("patientId");
        if (StringUtils.isEmpty(patientId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，患者不能为空!");
        }
        String queueId = requestParam.getString("queueId");
        if (StringUtils.isEmpty(queueId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，队列id不能为空!");
        }
        String type = requestParam.getString("type");
        if (StringUtils.isEmpty(type)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，问卷类型不能为空!");
        }
        String taskId = requestParam.getString("taskId");
        if (StringUtils.isEmpty(taskId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，任务id不能为空!");
        }
        String updateAccept = requestParam.getString("updateAccept");
        if (StringUtils.isEmpty(updateAccept)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参校验失败，操作流水不能为空!");
        }

        Map<String, Object> map = new HashMap<String, Object>();
        // 安医大app查询主诉登记特殊处理（无患者自助填写记录，则查询门诊记录）
        Map<String, Object> qryUserIn = new HashMap<String, Object>();
        qryUserIn.put("revTenantId", Constants.PT_USER_INFO_DEFAULT_TENANT_ID);
        qryUserIn.put("userId", patientId);// app侧必传
        qryUserIn.put("start", 0);
        qryUserIn.put("end", 1);
        List<Patient> appPtList = patientSerivce.getPatientList(qryUserIn);
        if (!CollectionUtils.isEmpty(appPtList)) {
            Patient appPtVo = appPtList.get(0);
            String appTenantId = appPtVo.getTenantId();
            if (Constants.TENANT_ID_FOR_AYD_MAS.equals(appTenantId)) {
                map.put("appTenantId", appTenantId);
            }
        }
        map.put("patientId", patientId);
        map.put("invokeType", "1");// app调用
        map.put("queueId", queueId);
        map.put("type", type);
        map.put("taskId", taskId);
        map.put("updateAccept", updateAccept);
        Map<String, Object> rspMap = appUserService.getPtOnlineOperateDetail(map);

        return new ResultModel<Map<String, Object>>(rspMap);
    }

    /**
     * 查询发送失败的短信
     * 
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/failSms/query")
    public QueryModel<Map<String, Object>> getFailSmsList(@RequestBody String jsonStr) throws Exception {
        logger.info("in getFailSmsList----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理

        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        String channel = requestParam.getString("channel");
        if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
        }

        List<Map<String, Object>> list = smsService.selectSendFailSmsList(requestParam);
        int total = 0;
        if (!CollectionUtils.isEmpty(list)) {
            total = smsService.selectSendFailSmsCount(requestParam);
        }

        logger.info("out getFailSmsList----arg==" + list);
        return new QueryModel<Map<String, Object>>(list, total);
    }

    // 更新app侧激活信息
    @ResponseBody
    @RequestMapping("/modifyPtAppStatus")
    public ResultModel<Object> modifyPtAppActStatus(@RequestBody String jsonStr, HttpServletRequest request)
            throws Exception {
        logger.warn("in modifyPtAppActStatus----arg==" + jsonStr);

        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }
        String channel = requestParam.getString("channel");
        if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
        }
        String userId = requestParam.getString("followUserId");
        String firstLogin = requestParam.getString("firstLogin");
        if (StringUtils.isEmpty(userId)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，患者id为空！");
        }
        String appActFlag = "";
        if (StringUtils.isEmpty(firstLogin)) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，激活标志为空！");
        } else if ("N".equals(firstLogin)) {
            appActFlag = "1";// 已激活
        } else if ("Y".equals(firstLogin)) {
            appActFlag = "0";// 未激活
        } else {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，激活标志传值错误！");
        }
        Map<String, Object> mapIn = new HashMap<String, Object>();
        mapIn.put("userId", userId);
        mapIn.put("appActFlag", appActFlag);
        mapIn.put("updateTime", DateUtil.getSysDateA());
        mapIn.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));

        Map<String, Object> outMap = appUserService.updatePtAppActStatus(mapIn);
        return new ResultModel<Object>(outMap);
    }
}
